package com.astrofizzbizz.raspberrypicamera.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class TimeLapseLatestImageViewerPanel extends VerticalPanel
{
	private RaspberryPiCamera entryPoint;
	private String latestImageName = "temp.jpg";
	private Image image;
	private Label latestImageLabel = new Label("");
	private int timerIntervalmS = 3000;
	private GetLatestImageTimer getLatestImageTimer;
	private LaunchTimeLapsePanel launchTimeLapsePanel;
	private Button killRaspistillButton = new Button("Kill raspistill process");
	private HorizontalPanel raspistillInfoPanel = new HorizontalPanel();
	
	public GetLatestImageTimer getGetLatestImageTimer() {return getLatestImageTimer;}
	
	public TimeLapseLatestImageViewerPanel(RaspberryPiCamera entryPoint)
	{
		super();
		this.entryPoint = entryPoint;
		launchTimeLapsePanel = new LaunchTimeLapsePanel(entryPoint);
		add(launchTimeLapsePanel);
		image = new Image("cameraImages/timeLapse/temp.jpg");
		image.setSize("800px", "600px");
		raspistillInfoPanel.setVerticalAlignment(ALIGN_MIDDLE);
		raspistillInfoPanel.add(latestImageLabel);
		raspistillInfoPanel.add(killRaspistillButton);
		killRaspistillButton.addClickHandler(new KillRaspistillButtonClickHandler());
		killRaspistillButton.setVisible(false);
		add(raspistillInfoPanel);
		image.setVisible(false);;
		add(image);
		getLatestImageTimer = new GetLatestImageTimer(timerIntervalmS);
		
	}
	private void updateLatestImageName()
	{
		String[] debugResponse = {"temp.jpg", "22"};
		entryPoint.getRaspberryPiService().getLatestTimeLapseImageName(entryPoint.isDebug(), debugResponse, new GetLatestTimeLapseImageNameCallback());
		
	}
	private void checkIfRaspistillIsRunning()
	{
		String[] debugResponse = {null,null};
		entryPoint.getRaspberryPiService().isRaspistillRunning(entryPoint.isDebug(), debugResponse, new IsRaspistillRunningCallback());
	}
	class IsRaspistillRunningCallback implements AsyncCallback<String[]>
	{
		@Override
		public void onFailure(Throwable caught) 
		{
			entryPoint.getStatusTextArea().addStatus("Error on IsRaspistillRunningCallback: " + caught.getMessage());
			launchTimeLapsePanel.setVisible(true);
			killRaspistillButton.setVisible(false);
		}
		@Override
		public void onSuccess(String[] result) 
		{
//			entryPoint.getStatusTextArea().addStatus("IsRaspistillRunningCallback " + result[0]);
			boolean stillRunning = true;
			if (result[0].indexOf("raspistill -o") < 0) stillRunning = false;
			launchTimeLapsePanel.setVisible(!stillRunning);
			killRaspistillButton.setVisible(stillRunning);
		}
	}
	class GetLatestTimeLapseImageNameCallback implements AsyncCallback<String[]>
	{

		@Override
		public void onFailure(Throwable caught) 
		{
//			entryPoint.getStatusTextArea().addStatus("Error on get latest timelapse Image: " + caught.getMessage());
			image.setVisible(false);;
		}

		@Override
		public void onSuccess(String[] result) 
		{
			if (result[0] != null)
			{
				latestImageName = "cameraImages/timeLapse/" + result[0];
				image.setUrl(latestImageName);
				image.setVisible(true);
				latestImageLabel.setText(result[0] + " is " + result[1] + " minutes old.");
			}
			else
			{
				image.setVisible(false);;
			}
		}
		
	}
	class KillRaspistillButtonClickHandler implements ClickHandler
	{
		public void onClick(ClickEvent event) 
		{
			String[] debugResponse = {null, null};
			killRaspistillButton.setVisible(false);
			entryPoint.getRaspberryPiService().killRaspistill(entryPoint.isDebug(), debugResponse, new KillRaspistillAsyncCallback());
		}
		
	}
	class KillRaspistillAsyncCallback implements AsyncCallback<String[]>
	{
		@Override
		public void onFailure(Throwable caught) 
		{
			entryPoint.getStatusTextArea().addStatus("Error KillRaspistillButtonClickHandler " + caught.getMessage());
		}
		@Override
		public void onSuccess(String[] result) 
		{
//			updateLatestImageName();
//			checkIfRaspistillIsRunning();
		}
		
	}
	class GetLatestImageTimer extends Timer
	{
		private boolean pause = true;
		private GetLatestImageTimer(int refresh_interval)
		{
			scheduleRepeating(refresh_interval);
		}

		@Override
		public void run() 
		{
			if (pause) return;
			updateLatestImageName();
			checkIfRaspistillIsRunning();
		}
		public void setPause(boolean pause) {this.pause = pause;}
		
	}

}
